package com.qtest.qandroid.controller;

import com.qtest.qandroid.dao.CourseDao;
import com.qtest.qandroid.entity.Course;
import com.qtest.qandroid.entity.Result;
import com.qtest.qandroid.service.CourseService;
import com.qtest.qandroid.util.RandomJoinNumber;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;

@RestController
public class CourseController {
    @Autowired
    private CourseService courseService;

    /**
     * 某个学生的所有课程
     * @param s
     * @return
     */
    @PostMapping ("/selectAll2")
    Result<List<Course>> selectAllCourseFromOneStudent(@RequestBody JSONObject s){
        String userId = s.getString("userId");
        Result<List<Course>> result =courseService.selectAllCourseFromOneStudent(userId);
        return result;
    }

    /**
     * 学生加入课程
     */
    @PostMapping("/isThereCorrectCode")
    public Result<Course> isThereCorrectCode(@RequestBody JSONObject s){
        String joinNumber=s.getString("joinNumber");
        String userId=s.getString("userId");
        Result<Course> result=new Result<>();
        Course course=courseService.isThereCorrectCode(joinNumber,userId );
        String courseId = String.valueOf(course.getCourseId());
        String name=course.getCourseName();
        if (course!=null){

            try{
                courseService.insertSelectCourse(userId, courseId, name);//插入selectCourse表一条记录
                result.setNum("1");
                result.setData(course);
                result.setMessage("加入成功");
            }catch(Exception e){
                result.setNum("1");
                result.setMessage("已加入课程");
                System.out.println("已加入课程");
            }

        }else{
            result.setNum("0");
            result.setMessage("该加课码不存在或已失效");
        }
        return result;

    }

    /**
     * 主界面教师新建课程
     */
    @PostMapping("/createNewCourse")
    public Result<Course> createNewCourse(@RequestBody JSONObject s){
        String userId=s.getString("userId");
        String js=s.getString("course");

        RandomJoinNumber r=new RandomJoinNumber();
        String joinNumber=r.createRandomJoinNumber();

        JSONObject JS = JSONObject.fromObject(js);
        Course course=(Course)JSONObject.toBean(JS, Course.class);

        course.setJoinNumber(joinNumber);

        Result<Course> result=new Result<>();


        courseService.createNewCourse(course);  //老师创建课程

        String courseId = String.valueOf(course.getCourseId());
        String name=course.getCourseName();

        courseService.insertSelectCourse(userId,courseId,name);//老师创建课程后，在选课表中增加一条记录
        System.out.println(course);

        if (course!=null){
            result.setNum("1");
            result.setData(course);
            result.setMessage("新建成功");
        }else{
            result.setNum("0");
            result.setMessage("新建失败,请重试");
        }
        return result;

    }

    /**
     * 教师删除课程
     */
    @PostMapping("/deleteCourse1")
    public Result<String> deleteCourse(@RequestBody JSONObject s){
        String courseId=s.getString("courseId");
        Result<String> result=new Result<>();
        courseService.deleteCourse(courseId);
        result.setNum("1");
        result.setMessage("删除成功");
        return result;
    }

    /**
     * 学生退出课程
     */
    @PostMapping("/deleteCourse2")
    public Result<String> deleteCourse2(@RequestBody JSONObject s){
        String userId=s.getString("userId");
        String courseId=s.getString("courseId");
        Result<String> result=new Result<>();
        courseService.deleteCourse2(userId,courseId);
        result.setNum("1");
        result.setMessage("删除成功");
        return result;
    }



}
